// devops/pages/pile/index.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    isShowBottom: false,
    hasNext:false,
    isRequest:true,
    showSearch: true,
    dataList: [],
    currentNav: 99,
    orderlist: '',
    visible: false,
    typeList: [
      {id: 'A', name: 'A'},
      {id: 'B', name: 'B'},
      {id: 'C', name: 'C'}
    ],
    phase: '',
    boxDeviceId: '',
    pileId: '',
    pidValue: [0,0,0],
    device: '',
    phone: '',
    sn: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },
  searchConfirm(){
    console.log('sear', this.data.phone)
    this.getList()
  },
  pageChange(val){
    console.log('组件传过来的值', val)
    this.setData({
      page: val.page
    })
    this.getList()
  },
  onClose(){
    this.setData({
      visible: false,
      pidValue: [0,0,0]
    })
  },
  confirmSite(option){
      this.setData({
      site: option.detail
    })
    this.getList()
  },
  confirmType(option){
    console.log('confirmType',option)
    this.setData({
      phase: this.data.typeList[option.detail].id,
    })
  },
  confirmDevice(option){
   console.log('confirmDevice',option)
    let arr = option.detail
    let pid = 0
    if(arr.three ==-99 || !arr.three){
     wx.showToast({
       title: '请选择三级配电箱',
       icon: 'none'
     })
     return
    }else{
      pid = arr.three
    }
    console.log('confirmDevice', pid)
    this.setData({
      boxDeviceId: pid,
    })
  },
    //列表
    getList(type) {
      console.log('刷新形式',type)
      let that = this
      //节流
      if(that.data.isRequest === false){
        return
      }
      this.setData({
        isRequest: false
      })
      let sear ={page: this.data.page}
      if(this.data.phone){
        sear = {...sear, phone: this.data.phone}
      }
      if(this.data.sn){
        sear = {...sear, sn: this.data.sn}
      }
      if(this.data.site){
        sear = {...sear, siteId: this.data.site}
      }
      service("POST","/devops/pile/list",sear).then(res => {
        console.log('个人桩列表', res)
        that.setData({
          isRefresh: false,
          isRequest: true
        })
        if(res.data.code ==0){
          let data = res.data.page.list
          that.setData({
            dataList: res.data.page.list,
            isRefresh: false,
            hasNext:res.data.page.hasNext,
          })
        }
      })
    },
    bandDevice(e){
      console.log('e',e)
      let id = e.currentTarget.dataset.id
      let device = e.currentTarget.dataset.device
      this.setData({
        pileId: id,
        visible: true,
        device: device
      })
      if(device){
        this.getPidArr(device)
      }
    },
    getPidArr(id){
      let that = this
      service("POST",`/devops/power/box/device/parent/${id}`,{}).then(res => {
        if(res.data.code ==0){
          let data = res.data.info
          let value = []
          console.log('shangji',data)
          if(data){
            value =  [data.parent.parent.id,data.parent.id, data.id]
          }
          that.setData({
            pidValue: value
          })
          console.log('上级设备',that.data.pidValue)
        }
      })
   },
    bandDeviceCon(){
      let that = this
      let pileId = this.data.pileId
      let device = this.data.device
      let phase = this.data.phase
      let boxDeviceId = this.data.boxDeviceId
      if(!phase || !boxDeviceId){
        wx.showToast({
          title: '请选择必填项',
          icon: 'none'
        })
        return
      }
      let url = '/devops/power/pile/bind'
      if(device){
        url = '/devops/power/pile/update'
      }
      console.log('',{pileId, phase, boxDeviceId})
      service("POST",url,{pileId, phase, boxDeviceId}).then(res => {
        if(res.data.code ==0){
          that.setData({
            visible: false
          })
          wx.showToast({
            title: '绑定成功',
            icon: 'none'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        that.getList(1)
        that.setData({
          device: ''
        })
      })
    },

      //自定义上拉加载
    loadMore(type) {
      //如果总页数 小于等于 当前页数 提示无更多数据
      if(!this.data.isRefresh){
        console.log('加载中')
        if (this.data.hasNext) {
          this.setData({
            page: this.data.page + 1,
            isRefresh:true
          })
          this.selectComponent("#orderlist").noMore('loading')
          this.getList(2)
        } else{
          console.log('没有更多')
          this.selectComponent("#orderlist").noMore('nomore')
          console.log('没有更多')
        }
      }
    },
    refresh(e){
      console.log('父组件刷新',e)
      this.setData({
        page: 1,
      })
      this.getList(1)
    },
    //查看报修订单
    goOrder(){
      wx.redirectTo({
        url: '/devops/pages/order/order',
      })
    },
    goRepair(){
      wx.redirectTo({
        url: '/devops/pages/repair/repair',
      })
    },
    goPower(){
      wx.redirectTo({
        url: '/devops/pages/power/power',
      })
    },
    goPile(){
      wx.redirectTo({
        url: '/devops/pages/pile/index',
      })
    },
    seeChart(e){
      let id = e.currentTarget.dataset.id
      let name = e.currentTarget.dataset.name
      let pileId = e.currentTarget.dataset.pileid
      console.log('e',e)
      // return
      wx.navigateTo({
        url: `/devops/pages/pile/gun?siteId=${id}&name=${name}&pileId=${pileId}`,
      })
    },
    setShow(){
      this.setData({
        showSearch: !this.data.showSearch
      })
    },
    clearSearch(){
      this.setData({
        phone: '',
        sn: '',
        site: ''
      })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let order = this.selectComponent("#orderlist")
    this.setData({
      orderlist: order
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})